package org.zhuxian.cn.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhuxian.cn.common.EquipmentSlot;
import org.zhuxian.cn.entity.Equipment;
import org.zhuxian.cn.mapper.EquipmentMapper;
import org.zhuxian.cn.service.EquipmentService;
import org.zhuxian.cn.utils.RedisUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void addEquipment(Equipment equipment) {
        // 使用 fromName 方法根据 name 属性查找枚举常量
        EquipmentSlot slot = EquipmentSlot.fromName(equipment.getEquipmentSlot());
        if (slot != null) {
            equipment.setPartType(slot.getType());
            equipmentMapper.insert(equipment);
        } else {
            // 处理找不到枚举常量的情况
            throw new IllegalArgumentException("未找到对应的装备槽: " + equipment.getEquipmentSlot());
        }
    }

    @Override
    public void modifyEquipment(Equipment equipment) {
        equipmentMapper.updateById(equipment);
    }

    @Override
    public void deleteEquipment(int id) {
        equipmentMapper.deleteById(id);
    }

    @Override
    public Equipment getEquipmentById(int id) {
        Equipment equipment = redisUtils.get("equipment:" + id, Equipment.class);
        if (equipment == null) {
            equipment = equipmentMapper.selectById(id);
            redisUtils.set("equipment:" + id, equipment);
        }
        return equipment;
    }

    @Override
    public List<Equipment> getAllEquipment() {
        List<Equipment> equipments = redisUtils.get("equipment:all", List.class);
        if (equipments == null || equipments.isEmpty()) {
            QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("quality_level");
            equipments = equipmentMapper.selectList(queryWrapper);

            equipments.forEach(equipment -> {
                redisUtils.set("equipment:" + equipment.getId(), equipment);
            });
            redisUtils.setEx("equipment:all", equipments, 3, TimeUnit.MINUTES);
        }
        return equipments;
    }

    @Override
    public List<Equipment> getEquipmentByClassAndSlot(String className, String slot) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("class_name", className);
        columnMap.put("equipment_slot", slot);

        // 调用 selectByMap 方法进行查询
        return equipmentMapper.selectByMap(columnMap);
    }

    @Override
    public List<Equipment> getEquipmentByClass(String className) {
        // 创建 QueryWrapper 对象
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        // 使用 or 方法拼接条件，查询指定职业或者 class_name 为通用的装备
        queryWrapper.eq("class_name", className).or().eq("class_name", "通用");
        queryWrapper.orderByAsc("quality_level");

        // 调用 selectList 方法进行查询
        return equipmentMapper.selectList(queryWrapper);
    }

    @Override
    public List<Equipment> getEquipmentByGeneral() {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_name", "通用");
        queryWrapper.orderByAsc("quality_level");

        // 调用 selectList 方法进行查询
        return equipmentMapper.selectList(queryWrapper);
    }
}
